﻿
#include "CreatePointDlg.h"

CreatePointDlg::CreatePointDlg(QWidget* parent):
    DlgBase(parent)
{

    initUI();
    manageConnections();
}

CreatePointDlg::~CreatePointDlg()
{
}

void CreatePointDlg::initUI()
{
    setWindowTitle("Create Point");
    QVBoxLayout* pDlgLayout = new QVBoxLayout(this);   //create layout

    {
        m_positionEdit = new MyCoordinateEdit(this, "Position");
        pDlgLayout->addWidget(m_positionEdit);
    }

    {
        m_okBtn = new QPushButton("OK", this);
        m_cancelBtn = new QPushButton("Cancel", this);

        QHBoxLayout* okCancelLayout = new QHBoxLayout(this);
        okCancelLayout->addWidget(m_okBtn);
        okCancelLayout->addWidget(m_cancelBtn);
        pDlgLayout->addLayout(okCancelLayout);
    }

    this->setLayout(pDlgLayout);
}

bool CreatePointDlg::create()
{
    auto pVtkViewWindow = MyVtkViewWindow::GetInstance();
    if(pVtkViewWindow == nullptr)
    {
        return false;
    }

    {
        m_positionEdit->getValues(m_position[0], m_position[1], m_position[2]);

        //vtk points
        vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
        points->InsertNextPoint(m_position);

        //vtk cell array
        vtkSmartPointer<vtkCellArray> cells = vtkSmartPointer<vtkCellArray>::New();
        vtkIdType pointIds[1] = {0};
        cells->InsertNextCell(1, pointIds);

        //vtk poly data
        vtkSmartPointer<vtkPolyData> pointPolyData = vtkSmartPointer<vtkPolyData>::New();
        pointPolyData->SetPoints(points);
        pointPolyData->SetVerts(cells);


        //vtk mapper
        vtkSmartPointer<vtkPolyDataMapper> pointMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
        pointMapper->SetInputData(pointPolyData);
        pointMapper->StaticOn();//设置静态属性，提高渲染效率(提升40%)


        //vtk actor
        vtkSmartPointer<vtkActor> pointActor = vtkSmartPointer<vtkActor>::New();
        pointActor->SetMapper(pointMapper);
        pointActor->GetProperty()->SetColor(QtVtkModle::Global::PointColor[0], QtVtkModle::Global::PointColor[1], QtVtkModle::Global::PointColor[2]);
        pointActor->GetProperty()->SetPointSize(QtVtkModle::Global::PointSize);
        pointActor->GetProperty()->VertexVisibilityOn();

        //add line to vtk view
        pVtkViewWindow->GetRenderer()->AddActor(pointActor);
        GetActorManagerInstance()->AddActor(pointActor, PickMode::POINT);
    }

    pVtkViewWindow->GetRenderer()->ResetCamera();
    pVtkViewWindow->GetRenderWindow()->Render();
    return true;
}

void CreatePointDlg::manageConnections()
{
    connect(m_okBtn, &QPushButton::clicked, this, [=]()
            {
                if(create())
                {
                    close();
                }
                else
                {
                    QMessageBox::warning(this, "Error", "Failed to create point!");
                }
            });

    connect(m_cancelBtn, &QPushButton::clicked, this, &CreatePointDlg::close);
}




